---
title: "`Actually' Marginal Effects with bigKRLS"
author: Pete Mohanty and Robert Shaffer
output: github_document
---

This code shows how well bigKRLS can do estimating 'actually' (as opposed to average) marginal effects without modeling any particular curve. (The true value of the derivative is shown in blue.) It can also be viewed [here](https://github.com/rdrr1990/bigKRLS/blob/master/examples/sinfx.md).

```{r, echo=FALSE, message=FALSE, warning=FALSE}
# all scripts in the replication materials assume bigKRLS 3.0.0 or higher
require(pacman)                   
p_load(bigKRLS, update = TRUE)
```

```{r, message=FALSE, warning=FALSE, comment=""}
library(bigKRLS)
library(ggplot2)
N <- 3000
P <- 2
set.seed(11112016)

X <- matrix(runif(N * P, -2*pi, 2*pi), ncol = P)
y <- sin(X[,1]) + X[,2] + rnorm(N)

out <- bigKRLS(y, X, noisy = FALSE, instructions = FALSE, eigtrunc = 0)

results <- data.frame(x1 = X[,1], delta = out$derivatives[,1], cosine=cos(X[,1]))
g <- ggplot(results, aes(x1, delta)) + geom_point(color="grey") 
g <- g + geom_line(aes(x1, cosine)) + theme_minimal(base_size = 18) + labs(x = "x1", y = "dy/dx1")
g

ggsave(g, file="sincurve.pdf")

```

Replication materials for "Messy Data, Robust Inference? Navigating Obstancles to Inference with bigKRLS"
By: Pete Mohanty (pmohanty@stanford.edu) and Robert Shaffer (rbshaffer@utexas.edu)
